/*
 * Copyright (c) 2013-2014 BlackBerry Limited.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "applicationui.hpp"

#include <bb/cascades/Application>
#include <bb/cascades/QmlDocument>
#include <bb/cascades/AbstractPane>
#include <bb/cascades/LocaleHandler>
#include <bb/system/InvokeManager>
#include <bb/system/InvokeRequest>
#include <bb/cascades/TabbedPane>
#include <bb/cascades/Tab>
#include "WebImageView.hpp"
#include "Setting.hpp"
#include "Favorites.hpp"
#include "QTimerOut.hpp"
#include "init.hpp"

using namespace bb::cascades;
using namespace bb::system;

ApplicationUI::ApplicationUI() :
        QObject(),
        m_translator(new QTranslator(this)),
        m_localeHandler(new LocaleHandler(this)),
        m_invokeManager(new InvokeManager(this))
{
    if (!QObject::connect(m_localeHandler, SIGNAL(systemLanguageChanged()),
            this, SLOT(onSystemLanguageChanged()))) {
        qWarning() << "Recovering from a failed connect()";
    }

    QObject::connect(m_invokeManager, SIGNAL(invoked(const bb::system::InvokeRequest&)),
                this, SLOT(onHandleInvoke(const bb::system::InvokeRequest&)));

    init *tmp = new init();
    tmp->executeInit();

    // initial load
    onSystemLanguageChanged();
    qmlRegisterType<Favorites>("bb.Favorites", 1, 0, "Favorites");
    qmlRegisterType<Favorites>("bb.Setting", 1, 0, "Setting");
    qmlRegisterType<QTimerOut>("bb.QTimerOut", 1, 0, "QTimerOut");
    qmlRegisterType<WebImageView>("bb.WebImageView", 1, 4, "WebImageView");

    // Create scene document from main.qml asset, the parent is set
    // to ensure the document gets destroyed properly at shut down.
    QmlDocument *qml = QmlDocument::create("asset:///main.qml").parent(this);

    // Make app available to the qml.
    qml->setContextProperty("app", this);

    Setting *ObjSetting = new Setting();
    qml->setContextProperty("_Setting", ObjSetting);

    Favorites *ObjFavorites = new Favorites();
    qml->setContextProperty("_Favorites", ObjFavorites);

    // Create root object for the UI
    AbstractPane *root = qml->createRootObject<AbstractPane>();

    // Set created root object as the application scene
    Application::instance()->setScene(root);
}

void ApplicationUI::onSystemLanguageChanged()
{
    QCoreApplication::instance()->removeTranslator(m_translator);
    // Initiate, load and install the application translation files.
    QString locale_string = QLocale().name();
    QString file_name = QString("iFengNews_%1").arg(locale_string);
    if (m_translator->load(file_name, "app/native/qm")) {
    QCoreApplication::instance()->installTranslator(m_translator);
    }
}

void ApplicationUI::restartService()
{
    InvokeRequest request;
    request.setTarget("com.example.iFengNewsService");
    request.setAction("com.example.iFengNewsService.RESET");

    QVariantMap map;
    map["opt"] = "restart";
    request.setMetadata(map);

    m_invokeManager->invoke(request);
    Application::instance()->minimize();
}

void ApplicationUI::onHandleInvoke(const bb::system::InvokeRequest &request){
    qDebug() << request.uri();
    emit invokeChanged(request.uri());
}

void ApplicationUI::addTab(){
    TabbedPane* tabbedPane = TabbedPane::create().showTabsOnActionBar(true);

    Tab* tab = Tab::create()
                 .title("tab1")
                 .description("This is only shown in the side bar")
                 .image(Image("tab.png"));

    tabbedPane->add(tab);

//    root->add(tab);
}
